/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.sys.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.sys.model.SysScoreConfig;
import com.yami.shop.sys.service.SysScoreConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


/**
 * 系统配置信息
 * @author lgh
 */
@RestController
@RequestMapping("/sys/scoreConfig")
public class SysScoreConfigController {
	@Autowired
	private SysScoreConfigService sysScoreConfigService;
	
	/**
	 * 所有配置列表
	 */
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('sys:scoreConfig:page')")
	public ResponseEntity<IPage<SysScoreConfig>> page(String paramKey,PageParam<SysScoreConfig> page){
		IPage<SysScoreConfig> sysConfigs = sysScoreConfigService.page(page, new LambdaQueryWrapper<SysScoreConfig>().like(StrUtil.isNotBlank(paramKey),SysScoreConfig::getParamKey,paramKey));
		return ResponseEntity.ok(sysConfigs);
	}
	
	
	/**
	 * 配置信息
	 */
	@GetMapping("/info/{id}")
	@PreAuthorize("@pms.hasPermission('sys:scoreConfig:info')")
	public ResponseEntity<SysScoreConfig> info(@PathVariable("id") Long id){
		SysScoreConfig config = sysScoreConfigService.getById(id);
		return ResponseEntity.ok(config);
	}
	
	/**
	 * 保存配置
	 */
	@SysLog("保存配置")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys:scoreConfig:save')")
	public ResponseEntity<Void> save(@RequestBody @Valid SysScoreConfig config){
		sysScoreConfigService.save(config);
		return ResponseEntity.ok().build();
	}
	
	/**
	 * 修改配置
	 */
	@SysLog("修改配置")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys:scoreConfig:update')")
	public ResponseEntity<Void> update(@RequestBody @Valid SysScoreConfig config){
		sysScoreConfigService.updateById(config);
		return ResponseEntity.ok().build();
	}
	
	/**
	 * 删除配置
	 */
	@SysLog("删除配置")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('sys:scoreConfig:delete')")
	public ResponseEntity<Void> delete(@RequestBody Long[] configIds){
		sysScoreConfigService.deleteBatch(configIds);
		return ResponseEntity.ok().build();
	}

}
